package cn.internship.modules.manage.controller;

import java.util.*;

import cn.internship.modules.manage.entity.StuInternCompanyEntity;
import cn.internship.modules.manage.entity.StuTeaRelationEntity;
import cn.internship.modules.manage.entity.dto.ReportSearch;
import cn.internship.modules.manage.entity.dto.StudentSearch;
import cn.internship.modules.manage.entity.vo.StudentDetail;
import cn.internship.modules.manage.service.StuInternCompanyService;
import cn.internship.modules.manage.service.StuTeaRelationService;
import cn.internship.modules.manage.service.TeacherInfoService;
import cn.internship.modules.sys.controller.AbstractController;
import cn.internship.modules.sys.entity.SysUserEntity;
import cn.internship.modules.sys.entity.SysUserRoleEntity;
import cn.internship.modules.sys.entity.dto.StudentInfoSave;
import cn.internship.modules.sys.service.SysUserRoleService;
import cn.internship.modules.sys.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.internship.modules.manage.entity.StudentInfoEntity;
import cn.internship.modules.manage.service.StudentInfoService;
import cn.internship.common.utils.PageUtils;
import cn.internship.common.utils.R;

import javax.annotation.Resource;


/**
 * 
 *
 * @author haha
 * @email haha@gmail.com
 * @date 2023-02-20 15:58:08
 */
@RestController
@RequestMapping("manage/studentinfo")
public class StudentInfoController extends AbstractController {
    @Autowired
    private StudentInfoService studentInfoService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private StuTeaRelationService stuTeaRelationService;

    @Resource
    private StuInternCompanyService stuInternCompanyService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("manage:studentinfo:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = studentInfoService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("manage:internshipreport:list")
    public R list(StudentSearch search){
        //获取用户ID
        Long userId = getUserId();
        //通过用户ID查询角色
        LambdaQueryWrapper<SysUserRoleEntity> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(SysUserRoleEntity::getUserId,userId);
        SysUserRoleEntity one = sysUserRoleService.getOne(queryWrap);
        if (one == null){
            return R.error("该用户不属于任何角色");
        }
        if (one.getRoleId() == 2){
            search.setTeaUserId(userId);
        }
        if (one.getRoleId() == 3){
            search.setStuUserId(userId);
        }
        PageUtils page = studentInfoService.listByPage(search);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:studentinfo:info")
    public R info(@PathVariable("id") Integer id){
		StudentInfoEntity studentInfo = studentInfoService.getById(id);

        return R.ok().put("studentInfo", studentInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:studentinfo:save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody StudentInfoSave studentsave){
        //默认分配学生账号
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(studentsave.getNo());
        //默认密码
        sysUserEntity.setPassword(studentsave.getNo());
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(3L);
        sysUserEntity.setRoleIdList(roleIds);
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setCreateUserId(getUserId());
        sysUserEntity.setMobile(studentsave.getPhone());
        sysUserEntity.setStatus(1);
        sysUserEntity.setEmail(studentsave.getEmail());
        sysUserService.saveUser(sysUserEntity);
        StudentInfoEntity studentInfo = new StudentInfoEntity();
        studentInfo.setUserId(sysUserEntity.getUserId());
        BeanUtils.copyProperties(studentsave,studentInfo);
        studentInfoService.save(studentInfo);
        StuTeaRelationEntity stuTeaRelationEntity = new StuTeaRelationEntity();
        stuTeaRelationEntity.setStuId(Long.parseLong(studentInfo.getId().toString()));
        stuTeaRelationEntity.setTeaId(Long.parseLong(studentsave.getTeaId().toString()));
        stuTeaRelationService.save(stuTeaRelationEntity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:studentinfo:update")
    public R update(@RequestBody StudentInfoEntity studentInfo){
		studentInfoService.updateById(studentInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:studentinfo:delete")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@RequestBody Integer[] ids){
		studentInfoService.removeByIds(Arrays.asList(ids));
        for (Integer id : ids) {
            StudentInfoEntity studentInfoEntity = studentInfoService.getById(id);
            sysUserService.removeById(studentInfoEntity.getUserId());
        }
        return R.ok();
    }

    @RequestMapping("/detail/{id}")
    @RequiresPermissions("manage:studentinfo:detail")
    public Object detail(@PathVariable("id") Integer id){
        StudentDetail detail = studentInfoService.getDetail(id);
        LambdaQueryWrapper<StuInternCompanyEntity> queryWrap = new LambdaQueryWrapper<>();
        queryWrap.eq(StuInternCompanyEntity :: getStuId,id);
        detail.setCompanys(stuInternCompanyService.list(queryWrap));
        return R.ok().put("detail",detail);
    }


}
